<template>
	<view class="addEmployee">
		<uni-forms :rules="rulesWxForm" ref="form" :modelValue="form" label-position="top">
	
	
	
	
			<uni-forms-item label="手机号" name="merchantCode" required labelWidth="180px">
				<view class="iphone">
					<uni-easyinput v-model="form.merchantCode" placeholder="请输入手机号" />
					 <view class="verificationCode" @click="getVerificationCode()">
					 	{{count?count+'秒':'获取验证码'}}
					 </view>
				</view>
			</uni-forms-item>
			<uni-forms-item label="验证码" name="code" required labelWidth="180px">
				<uni-easyinput  maxlength="6" v-model="form.code" placeholder="请输入验证码" />
	
			</uni-forms-item>
			<uni-forms-item label="新密码" name="newPassword" required labelWidth="180px">
				<uni-easyinput type="password" v-model="form.newPassword" placeholder="请输入新密码" />
	
			</uni-forms-item>
			<uni-forms-item label="确定新密码" name="againNewPassword" required labelWidth="180px">
				<uni-easyinput type="password" v-model="form.againNewPassword" placeholder="请再次输入新密码" />
	
			</uni-forms-item>
		
			<view class="sumbit-btn flex" @click.stop="saveInfo('form')">
				修改密码
			</view>
	
	
		</uni-forms>
	</view>
</template>

<script>
	export default {
		data() {
			return {
			     // 商家id
				 merchantId: uni.getStorageSync('merchantId'),
		
				// 商家注册
				form:{
					// 商家账号
					merchantCode:'',
				
					// 新密码
					newPassword:'',
					// 再次输入新密码
					againNewPassword:'',
						code:'',
				
				},
				count:0,
				disableCode:false,
			
				codeVerification:'',
				rulesWxForm: {
					merchantCode: {
						rules: [{
							required: true,
							errorMessage: '请输入手机号'
						}]
					},
					phone: {
						rules: [{
							required: true,
							errorMessage: '请输入联系电话'
						}]
					},
					code: {
						rules: [{
							required: true,
							errorMessage: '请输入登验证码'
						}]
					},
					newPassword:{
						rules: [{
							required: true,
							errorMessage: '请输入新密码'
						}]
					
					},
					againNewPassword:{
						rules: [{
							required: true,
							errorMessage: '请再次输入新密码'
						}]
					
					}
				
				},
				
				
			}
		},
		methods:{
			// 验证码码倒计60秒
			 countDown(seconds) {
			   this.count = seconds;
			  var countdownTimer = setInterval(()=> {
			  
			    this.count--;
			    if (this.count === 0) {
			      clearInterval(countdownTimer);
			      this.disableCode=false
			      // 在这里可以触发验证码过期后的操作
			    }
			  }, 1000);
			},
			// 登录
			async getVerificationCode(){
				if(!this.disableCode){
					console.log(this.form.merchantCode)
					if(/^1[3456789]\d{9}$/.test(this.form.merchantCode)){
						const result = await this.$http.get({
							url: this.$api.sendSmsRequest,
							data: {
								domainName: 'https://yuntt.weijuyunke.com',
								phone:this.form.merchantCode
								
							}
						});
						
						if(result.code==2000){
							this.codeVerification=result.data
							console.log(this.codeVerification)
							this.disableCode=true
							this.countDown(60);
						}else{
							this.$sun.toast(res.msg,'none')
						}
						
					}else{
						this.$sun.toast('请填写正确的手机号','none')
					}
				}
			
			
			},
		saveInfo(formName) {
			console.log(formName)
			this.$refs[formName].validate((err, formData) => {
				console.log(err, formData)
				if (err) {
					console.log(err, formData)
					return
				}else{
					this.$at.uniDoFunc({
						key: 'release',
						success: async () => {
							this.$at.lockKey('release');
						  console.log(Number(this.codeVerification)===Number(this.form.code))
						 if(!(Number(this.codeVerification)===Number(this.form.code))){
						 	this.$sun.toast("输入验证码不正确",'none');
							 this.$at.releaseKey('release');
						 	return;
						 }
						 if(!(Number(this.form.newPassword)===Number(this.form.againNewPassword))){
						 	this.$sun.toast("二次输入的密码不正确",'none');
							 this.$at.releaseKey('release');
						 	return;
						 }
						 
						 const result = await this.$http.post({
						 	url: this.$api.updatePassword,
						 	data: {
						 		id:this.merchantId,
						 		merchantCode:this.form.merchantCode,
						 		password:this.form.againNewPassword,
						 	},
						 
						 
						 });
						 
						 if(result.code==2000){
							 setTimeout(()=>{
								 this.$sun.toast('修改成功','none')
							 },500)
							 this.$sun.navRe('/pages/index/my');
							 this.$at.releaseKey('release');
							 
						 }else{
							 this.$sun.toast(result.msg,'none')
							 this.$at.releaseKey('release');
						 }
					       
						}
					});
				}
			
			
			}).then(async res => {
				this.$at.uniDoFunc({
					key: 'release',
					success: async () => {
						this.$at.lockKey('release');
					
		
		               this.$at.releaseKey('release');
					}
				});
		
		
			})
		
		},
		}
	}
	
</script>

<style lang="less" scoped>
	page{
		background-color: #fff;
	}
	.addEmployee{
		padding: 45rpx;
		.iphone{
			position: relative;
			.verificationCode{
				z-index: 999;
				position: absolute;
				top: 5rpx;
				right: 5rpx;
				text-align: center;
				width: 170rpx;
				height: 65rpx;
				line-height: 65rpx;
				border-radius: 100rpx;
				font-size: 20rpx;
				background: #1E6CEB;
				color: #FFF;
			}
		}
	
		.sumbit-btn {
			width: 670rpx;
			position: fixed;
		
			bottom: 50rpx;
		
			height: 100rpx;
			color: #fff;
			border-radius: 10px;
		
			background-color: #1890FF;
		}
	}

</style>